package com.heo.protocol.packet;

import org.apache.log4j.Logger;
import com.heo.util.ByteHelper;

/**
 * 服务器主动下发，要求设备上传日志
 * 
 * @author chichi
 */
public class SetUploadLogReq extends BasePacket
{
  private int BODY_LENGTH = 10;// 包体长10个字节
  private static final String PACKET_NAME = "[要求设备上传日志请求包]";

  private static Logger log = Logger.getLogger(SetUploadLogReq.class);

  private long logEndTime;// 要求上传的日志截止时间
  private int logLength;// 要求上传的日志长度

  /**
   * 通过网络传过来的流构建对象
   * 
   * @param totalLength
   * @param commandId
   * @param sequenceId
   * @param body
   * @throws Exception
   */
  public SetUploadLogReq(String logHeader, SocketPacket packet) throws Exception
  {
    super.copyPacket(logHeader, PACKET_NAME, packet);
    makeBody();
  }
  
  @Override
  public int getTotalLength()
  {
    return super.HEAD_LENGTH + BODY_LENGTH;
  }
  
  public SetUploadLogReq(long logEndTime,int logLength, int sequenceId) throws Exception
  {
    super(PACKET_NAME);
    this.logEndTime = logEndTime;
    this.logLength = logLength;
    super.totalLength = (short) (HEAD_LENGTH + BODY_LENGTH);
    super.commandId = DeviceCommand.SET_UPLOAD_LOG_REQ;
    super.sequenceId = sequenceId;
  }

  @Override
  public byte[] getPacketBody()
  {
    return ByteHelper.margeByte(ByteHelper.longToByteArray(logEndTime), ByteHelper.shortToByteArray((short)logLength), null, null, null, null, null, null, null, null);
  }

  @Override
  public void makeBody() throws Exception
  {
    if (packetBody.length != BODY_LENGTH)
    {
      log.error(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
      throw new Exception(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
    }
    log.debug(super.logHeader + super.packetName + "数据包解析成功。");
  }

  public static void main(String[] args)
  {

  }

  public long getLogEndTime()
  {
    return logEndTime;
  }

  public void setLogEndTime(long logEndTime)
  {
    this.logEndTime = logEndTime;
  }

  public int getLogLength()
  {
    return logLength;
  }

  public void setLogLength(int logLength)
  {
    this.logLength = logLength;
  }

}
